package com.call.project.jni;

import com.call.project.utils.OSinfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 Created by IntelliJ IDEA.
 User: wk
 Date: 2017-03-20 下午3:54
 */
public class CInterface {
    public static final int END    = 65520;
    private static Logger   LOGGER = LoggerFactory.getLogger(CInterface.class);
    private MsgCallBack     msgCallBack;

    static {
        if (OSinfo.isWindows()) {
            InputStream in = null;
            FileOutputStream out = null;
            File dll = null;
            try {
                in = new ClassPathResource("dll/CallLibrary.dll").getInputStream();
                dll = File.createTempFile("CallLibrary", ".dll");
                out = new FileOutputStream(dll);
                int i;
                byte[] buf = new byte[1024];
                while ((i = in.read(buf)) != -1) {
                    out.write(buf, 0, i);
                }
                out.flush();
                dll.deleteOnExit();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException e) {

                    }
                if (out != null)
                    try {
                        out.close();
                    } catch (Exception e) {
                    }
            }
            if (dll != null)
                System.load(dll.toString());
        }

    }

    public native int getStatus();

    /***
     *
     * @param portNumber  串口 默认 3a
     * @param rate 波特率 默认 1200
     * @param partity 奇偶校验 Y or N  默认 N
     * @param databits 数据位 默认8
     * @param stopbits 停止位 默认1
     * @return
     */
    public native boolean initServer(int portNumber, int rate, char partity, int databits, int stopbits);

    public native void sendMsg(char[] command);

    public native int registerWindows();
    /**
     * 拿到硬盘号
     * @return
     */
    public native long getNumber();
    /**
     * 拿到是否注册
     * @return
     */
    public native int isRegister();

    /**
     * 是否已经过期
     * @return
     */
    public native int isExpire();

    /**
     * 调用windows说话
     * @param msg
     */
    public native void speek(String msg);

    public void callback(char msg) {
        if (msg != END) {
            if (msgCallBack != null)
                msgCallBack.sendMsg(msg);
        }
    }

    public void destory(String msg) {
        LOGGER.warn(msg + "################");
    }

    public void timePing(long time) {
        LOGGER.info("time is {} ", time);
    }

    public MsgCallBack getMsgCallBack() {
        return msgCallBack;
    }

    public void setMsgCallBack(MsgCallBack msgCallBack) {
        this.msgCallBack = msgCallBack;
    }
}
